<template>
  <div class="meedu-main-body">
    <backBar class="mb-30" title="编辑自动回复"></backBar>

    <el-form
      ref="form"
      class="float-left"
      :model="reply"
      :rules="rules"
      label-width="200px"
      v-if="reply"
    >
      <el-form-item label="消息类型" prop="type">
        <el-select v-model="reply.type" class="w-200px">
          <el-option
            v-for="(item, index) in types"
            :key="index"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="触发关键字" v-show="reply.type === 'text'">
        <div class="d-flex">
          <div>
            <el-input
              v-model="reply.rule"
              class="w-200px"
              placeholder="请输入触发关键字"
            ></el-input>
          </div>
          <div class="ml-10">
            <helperText text="支持正则表达式"></helperText>
          </div>
        </div>
      </el-form-item>

      <el-form-item label="事件" v-show="reply.type == 'event'">
        <el-select v-model="reply.event_type" class="w-200px">
          <el-option
            v-for="(item, index) in events"
            :key="index"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="事件key" v-if="reply.type == 'event'">
        <el-input v-model="reply.event_key" class="w-400px"></el-input>
      </el-form-item>

      <el-form-item label="回复内容" prop="reply_content">
        <el-input
          class="w-500px"
          type="textarea"
          rows="4"
          placeholder="请输入回复内容"
          v-model="reply.reply_content"
        ></el-input>
      </el-form-item>
    </el-form>

    <div class="bottom-menus">
      <div class="bottom-menus-box">
        <div>
          <el-button @click="UpDate" type="primary">保存</el-button>
        </div>
        <div class="ml-24">
          <el-button @click="$router.back()">取消</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import backBar from "@/components/back-bar.vue";
import helperText from "@/components/helper-text.vue";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {
    backBar,
    helperText,
  },
  data() {
    //这里存放数据
    return {
      id: this.$route.query.id,
      reply: null,
      rules: {
        type: [
          {
            required: true,
            message: "消息类型不能为空",
            trigger: "blur",
          },
        ],
        reply_content: [
          {
            required: true,
            message: "回复内容不能为空",
            trigger: "blur",
          },
        ],
      },
      types: [],
      events: [
        {
          id: "subscribe",
          name: "用户关注",
        },
        {
          id: "CLICK",
          name: "自定义菜单事件",
        },
      ],
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    getCreate() {
      this.$request
        .get(`mpWechatMessageReply/create`)
        .then((res) => {
          this.types = res.data.types;
        })
        .catch((e) => {
          this.$message.error(e.message);
        });
    },
    UpDetail() {
      this.$request
        .get(`mpWechatMessageReply/${this.id}`)
        .then((res) => {
          this.reply = res.data.data;
        })
        .catch((e) => {
          this.$message.error(e.message);
        });
    },
    UpDate() {
      this.$request
        .put(`mpWechatMessageReply/${this.id}`, this.reply)
        .then((res) => {
          if (res.status == 500 || res.status == 1) {
            this.$message.error(res.message);
          } else {
            this.$message.success(this.$t("common.success"));
            this.$router.back();
          }
        })
        .catch((e) => {
          this.$message.error(e.message);
        });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.getCreate();
    this.UpDetail();
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='less'  scoped>
:deep(.el-textarea__inner) {
  width: 500px;
}
</style>